package seekting.demo2017.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.seekting.demo_lib.MainActivity;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import seekting.demo2017.AppEnv;

/**
 * Created by seekting on 17-5-18.
 */

public class SimpleTestActivity extends Activity {

    public static final boolean DEBUG = AppEnv.bAppdebug;
    public static final String TAG = SimpleTestActivity.class.getSimpleName();

    static {

        MainActivity.TITLE_MAP.put(SimpleTestActivity.class, "");
        MainActivity.DESC_MAP.put(SimpleTestActivity.class, "");
    }

    private ImageView mImageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (DEBUG) {
            Log.d(TAG, "onCreate.");
        }
        mImageView = new ImageView(this);
        setContentView(mImageView);
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://image.i.360overseas.com/Uploads/201703/720x1280_58d33e4fd2e53.webp";
                try {
                    URL iconUrl = new URL(url);
                    URLConnection conn = iconUrl.openConnection();
                    HttpURLConnection http = (HttpURLConnection) conn;

                    int length = http.getContentLength();

                    conn.connect();
                    // 获得图像的字符流
                    InputStream is = conn.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is, length);
                    final Bitmap bm = BitmapFactory.decodeStream(bis);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            int color = bm.getPixel(100, 100);

                            if (DEBUG) {
                                Log.d(TAG, "onCreate.bm" + bm.getWidth() + "color" + color);
                            }
                            mImageView.setBackgroundDrawable(new BitmapDrawable(bm));
                        }
                    });

                    bis.close();
                    is.close();// 关闭流
                } catch (Throwable t) {
                    if (DEBUG) {
                        Log.d(TAG, "onCreate.", t);
                    }

                }
            }
        }).start();


    }
}
